In the previous exercise, we already have created our first loop using an if-else statement. Here we want to deepen our understanding of loops and apply some more of them.

1

Take the list you have created in the previous exercise. Use your fancy function to check whether an object is numeric and apply it to each of the list elements. Use a for() loop for this purpose.
When your list is not in your environment anymore, you have to re-create it again. Check out our solution if you want to use a hideous one. Oh… the same applies to the function.

for() loops are nice, but there are alternatives, right?

2

Now use the lapply() function to iterate through the list - do you see any differences to the for() call?
Look at the number of elements to search for differences.

To conclude this exercise, let’s turn to code styling. For this purpose, look at this code fragment here:

mean(sqrt(as.numeric(data.frame(id = 1, age = "20")$age)))

3

What do you think the command does?
It’s always an excellent approach to start reading/interpreting code from the central/inner command and continue to the outer ones.

Using the commands in such a way makes the code somewhat challenging to read and understand. You should have learned that pipes provide a straightforward approach to address this issue.

4

Create a pipe from this nested command.
You can call individual columns of a piped object with .$col_name here.